package com.luobu.javaee.controller;

import com.luobu.javaee.domain.Archive;
import com.luobu.javaee.domain.Document;
import com.luobu.javaee.domain.dto.ArchiveIdsAndUserId;
import com.luobu.javaee.domain.dto.DocumentDto;
import com.luobu.javaee.domain.dto.DocumentResDto;
import com.luobu.javaee.domain.dto.ImgResDto;
import com.luobu.javaee.exception.DocumentException;
import com.luobu.javaee.service.DocumentElasticSearchService;
import com.luobu.javaee.service.DocumentService;
import com.luobu.javaee.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/doc")
public class DocController {
    @Autowired
    DocumentService documentService;

    @Autowired
    ImageService imageService;

    @Autowired
    DocumentElasticSearchService documentElasticSearchService;

    // 上传文件,Body为DocumentDto
    @PostMapping("/uploadDoc")
    public ResponseEntity<Void> addDoc(@RequestBody DocumentDto documentDto) throws DocumentException, IOException {
        documentService.addDoc(documentDto);
        documentElasticSearchService.upload(documentDto);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/updateDoc")
    public ResponseEntity<Void> updateDoc(@RequestBody DocumentDto documentDto) throws DocumentException, IOException {
        documentService.updateDoc(documentDto);
        documentElasticSearchService.upload(documentDto);
        return ResponseEntity.ok().build();
    }
    // 以表单方式上传图片
    /*
    @PostMapping(value = "/uploadPic1", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public String uploadFiles(@RequestParam("id") String[] ids, @RequestParam("file") MultipartFile[] files) {
        List<ImgResDto> res = imageService.uploadPic(files, userId, docName);
        return ResponseEntity.ok(res);
    }
    */

    @PostMapping("/uploadPic")
    @ResponseBody
    public ResponseEntity<List<ImgResDto>> upload(@RequestParam("files") List<MultipartFile> files,
                                                  @RequestParam("userId") Long userId,
                                                  @RequestParam("docName") String docName,
                                                  @RequestParam("archiveId") Long archiveId) throws DocumentException, IOException {
        List<ImgResDto> res = imageService.uploadPic(files, userId, docName, archiveId);
        return ResponseEntity.ok(res);
    }

    @GetMapping("/searchImg")
    public ResponseEntity<List<String>> searchImg(Long userId, String docName) {
        List<String> picResults = imageService.encodeAllPicsBase64(userId, docName);
        return ResponseEntity.ok(picResults);
    }

    @DeleteMapping("/deleteDoc")
    public ResponseEntity<Void> deleteDoc(Long id) throws DocumentException, IOException {
        System.out.println("进入删除命令，文档id："+id);
        documentService.deleteDoc(id);
        System.out.println("删除命令完成");
        System.out.println("删除在es中的记录");
        documentElasticSearchService.deleteByDocId(id);
        System.out.println("删除在es中的记录成功");
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getUserArchiveDoc")
    public ResponseEntity<List<DocumentResDto>> getUserArchiveDoc(Long userId, Long archiveId) throws DocumentException, IOException{
        List<DocumentResDto> documentResDtos = documentService.getUserArchiveDoc(userId, archiveId);
        return ResponseEntity.ok(documentResDtos);
    }

    // 获取用户的所有文件列表
    @GetMapping("/getUserDocList")
    public ResponseEntity<List<DocumentResDto>> getList(Long userId) throws DocumentException, IOException {
        //List<DocumentResDto> documentResDtos = documentService.getList(userId);
        //return ResponseEntity.ok(documentResDtos);
        return null;
    }

    @GetMapping("/getShareList")
    public ResponseEntity<List<Archive>> getShareList(String userName) throws DocumentException, IOException {
        List<Archive> archives = documentService.getShareList(userName);
        return ResponseEntity.ok(archives);
    }

    @PostMapping("/shareDoc")
    public ResponseEntity<Void> shareDoc(@RequestBody ArchiveIdsAndUserId archiveIdsAndUserId) throws DocumentException, IOException {
        documentService.shareArchives(archiveIdsAndUserId.getArchiveIds(), archiveIdsAndUserId.getUserName());
        return ResponseEntity.ok().build();
    }

}
